package com.mall.order.biz.factory;

import com.mall.order.biz.handler.*;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.biz.convert.CreateOrderConvert;
import com.mall.order.biz.convert.TransConvert;
import com.mall.order.dto.CreateOrderRequest;
import com.mall.order.biz.handler.ClearCartItemHandler;
import com.mall.order.biz.handler.SendMessageHandler;
import com.mall.order.biz.handler.SubStockHandler;
import com.mall.order.biz.handler.ValidateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 构建订单创建处理链   订单的一个pipeline
 */
@Slf4j
@Service
public class OrderProcessPipelineFactory extends AbstranctTransPipelineFactory<CreateOrderRequest> {

    /**
     * DI 注入handler组件
     */
    @Autowired
    private InitOrderHandler initOrderHandler;   //初始化订单
    @Autowired
    private ValidateHandler validateHandler;    //验证用户合法性
    @Autowired
    private LogisticalHandler logisticalHandler;   //处理物流信息
    @Autowired
    private ClearCartItemHandler clearCartItemHandler;  //清空购物车
    @Autowired
    private SubStockHandler subStockHandler;  //扣减库存
    @Autowired
    private SendMessageHandler sendMessageHandler;   //利用mq发送延迟取消订单消息


    @Override
    protected TransHandlerContext createContext() {
        CreateOrderContext createOrderContext=new CreateOrderContext();
        return createOrderContext;
    }

    /**
     * 将容器中的Handler组件放入管道尾部
     * @param pipeline
     */
    @Override
    protected void doBuild(TransPipeline pipeline) {   //context作为一个产品（包含需要的数据），从上至下，不断封装进新的数据（所以：顺序的优先级别需要排列好）
        pipeline.addLast(validateHandler);
        pipeline.addLast(subStockHandler);
        pipeline.addLast(initOrderHandler);
        pipeline.addLast(logisticalHandler);
        pipeline.addLast(clearCartItemHandler);
        pipeline.addLast(sendMessageHandler);
    }

    /**
     * 创建转换器
     * @return
     */
    @Override
    protected TransConvert createConvert() {
        return new CreateOrderConvert();
    }
}
